package controllers

import (
	"context"
	"fmt"
	"meituan/utils/random"
	"regexp"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
)

// Sms 发送验证码
func Sms(ctx *gin.Context) {
	body := make(map[string]string)
	ctx.ShouldBindJSON(&body)
	phone, _ := body["phone"]
	fmt.Println(phone)
	matched, error := regexp.Match("\\d{11}", []byte(phone))
	if !matched || error != nil {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "请输入正确得手机号码",
		})
		return
	}
	code := random.GenCode(6)
	client := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		DB:   1,
	})
	client.Set(context.Background(), phone, code, time.Second*300)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "发送成功",
	})
}
